package com.j256.ormlite.stmt.mapped;

import com.j256.ormlite.dao.ObjectCache;
import com.j256.ormlite.db.DatabaseType;
import com.j256.ormlite.field.FieldType;
import com.j256.ormlite.logger.Log.Level;
import com.j256.ormlite.logger.Logger;
import com.j256.ormlite.misc.SqlExceptionUtil;
import com.j256.ormlite.support.DatabaseConnection;
import com.j256.ormlite.support.GeneratedKeyHolder;
import com.j256.ormlite.table.TableInfo;
import java.sql.SQLException;

public class MappedCreate<T, ID> extends BaseMappedStatement<T, ID>
{
  private String dataClassName;
  private final String queryNextSequenceStmt;
  private int versionFieldTypeIndex;

  private MappedCreate(TableInfo<T, ID> paramTableInfo, String paramString1, FieldType[] paramArrayOfFieldType, String paramString2, int paramInt)
  {
    super(paramTableInfo, paramString1, paramArrayOfFieldType);
    this.dataClassName = paramTableInfo.getDataClass().getSimpleName();
    this.queryNextSequenceStmt = paramString2;
    this.versionFieldTypeIndex = paramInt;
  }

  private void assignIdValue(T paramT, Number paramNumber, String paramString, ObjectCache paramObjectCache)
    throws SQLException
  {
    this.idField.assignIdValue(paramT, paramNumber, paramObjectCache);
    if (logger.isLevelEnabled(Log.Level.DEBUG))
    {
      Logger localLogger = logger;
      Object[] arrayOfObject = new Object[4];
      arrayOfObject[0] = paramNumber;
      arrayOfObject[1] = paramString;
      arrayOfObject[2] = this.idField.getFieldName();
      arrayOfObject[3] = this.dataClassName;
      localLogger.debug("assigned id '{}' from {} to '{}' in {} object", arrayOfObject);
    }
  }

  private void assignSequenceId(DatabaseConnection paramDatabaseConnection, T paramT, ObjectCache paramObjectCache)
    throws SQLException
  {
    long l = paramDatabaseConnection.queryForLong(this.queryNextSequenceStmt);
    logger.debug("queried for sequence {} using stmt: {}", Long.valueOf(l), this.queryNextSequenceStmt);
    if (l == 0L)
      throw new SQLException("Should not have returned 0 for stmt: " + this.queryNextSequenceStmt);
    assignIdValue(paramT, Long.valueOf(l), "sequence", paramObjectCache);
  }

  public static <T, ID> MappedCreate<T, ID> build(DatabaseType paramDatabaseType, TableInfo<T, ID> paramTableInfo)
  {
    StringBuilder localStringBuilder = new StringBuilder(128);
    appendTableName(paramDatabaseType, localStringBuilder, "INSERT INTO ", paramTableInfo.getTableName());
    int i = 0;
    int j = -1;
    for (FieldType localFieldType2 : paramTableInfo.getFieldTypes())
      if (isFieldCreatable(paramDatabaseType, localFieldType2))
      {
        if (localFieldType2.isVersion())
          j = i;
        i++;
      }
    FieldType[] arrayOfFieldType2 = new FieldType[i];
    if (i == 0)
      paramDatabaseType.appendInsertNoColumns(localStringBuilder);
    int i3;
    while (true)
    {
      String str = buildQueryNextSequence(paramDatabaseType, paramTableInfo.getIdField());
      return new MappedCreate(paramTableInfo, localStringBuilder.toString(), arrayOfFieldType2, str, j);
      int n = 1;
      localStringBuilder.append('(');
      FieldType[] arrayOfFieldType3 = paramTableInfo.getFieldTypes();
      int i1 = arrayOfFieldType3.length;
      int i2 = 0;
      i3 = 0;
      while (i2 < i1)
      {
        FieldType localFieldType1 = arrayOfFieldType3[i2];
        int i7;
        if (!isFieldCreatable(paramDatabaseType, localFieldType1))
        {
          i7 = i3;
          i2++;
          i3 = i7;
        }
        else
        {
          if (n != 0)
            n = 0;
          while (true)
          {
            appendFieldColumnName(paramDatabaseType, localStringBuilder, localFieldType1, null);
            i7 = i3 + 1;
            arrayOfFieldType2[i3] = localFieldType1;
            break;
            localStringBuilder.append(",");
          }
        }
      }
      localStringBuilder.append(") VALUES (");
      int i4 = 1;
      FieldType[] arrayOfFieldType4 = paramTableInfo.getFieldTypes();
      int i5 = arrayOfFieldType4.length;
      int i6 = 0;
      while (i6 < i5)
        if (!isFieldCreatable(paramDatabaseType, arrayOfFieldType4[i6]))
        {
          i6++;
        }
        else
        {
          if (i4 != 0)
            i4 = 0;
          while (true)
          {
            localStringBuilder.append("?");
            break;
            localStringBuilder.append(",");
          }
        }
      localStringBuilder.append(")");
    }
  }

  private static String buildQueryNextSequence(DatabaseType paramDatabaseType, FieldType paramFieldType)
  {
    if (paramFieldType == null);
    String str;
    do
    {
      return null;
      str = paramFieldType.getGeneratedIdSequence();
    }
    while (str == null);
    StringBuilder localStringBuilder = new StringBuilder(64);
    paramDatabaseType.appendSelectNextValFromSequence(localStringBuilder, str);
    return localStringBuilder.toString();
  }

  private boolean foreignCollectionsAreAssigned(FieldType[] paramArrayOfFieldType, Object paramObject)
    throws SQLException
  {
    int i = paramArrayOfFieldType.length;
    for (int j = 0; j < i; j++)
      if (paramArrayOfFieldType[j].extractJavaFieldValue(paramObject) == null)
        return false;
    return true;
  }

  private static boolean isFieldCreatable(DatabaseType paramDatabaseType, FieldType paramFieldType)
  {
    if (paramFieldType.isForeignCollection());
    do
    {
      do
        return false;
      while (paramFieldType.isReadOnly());
      if ((paramDatabaseType.isIdSequenceNeeded()) && (paramDatabaseType.isSelectSequenceBeforeInsert()))
        return true;
    }
    while ((paramFieldType.isGeneratedId()) && (!paramFieldType.isSelfGeneratedId()) && (!paramFieldType.isAllowGeneratedIdInsert()));
    return true;
  }

  public int insert(DatabaseType paramDatabaseType, DatabaseConnection paramDatabaseConnection, T paramT, ObjectCache paramObjectCache)
    throws SQLException
  {
    FieldType localFieldType1 = this.idField;
    KeyHolder localKeyHolder = null;
    int n;
    if (localFieldType1 != null)
    {
      if ((!this.idField.isAllowGeneratedIdInsert()) || (this.idField.isObjectsFieldValueDefault(paramT)))
        break label143;
      n = 0;
    }
    while (true)
    {
      if ((this.idField.isSelfGeneratedId()) && (this.idField.isGeneratedId()))
      {
        localKeyHolder = null;
        if (n != 0)
          this.idField.assignField(paramT, this.idField.generateId(), false, paramObjectCache);
      }
      try
      {
        label84: if (this.tableInfo.isForeignAutoCreate())
        {
          FieldType[] arrayOfFieldType = this.tableInfo.getFieldTypes();
          int k = arrayOfFieldType.length;
          int m = 0;
          label111: if (m < k)
          {
            FieldType localFieldType3 = arrayOfFieldType[m];
            boolean bool1 = localFieldType3.isForeignAutoCreate();
            if (!bool1);
            while (true)
            {
              m++;
              break label111;
              n = 1;
              break;
              if ((this.idField.isGeneratedIdSequence()) && (paramDatabaseType.isSelectSequenceBeforeInsert()))
              {
                localKeyHolder = null;
                if (n == 0)
                  break label84;
                assignSequenceId(paramDatabaseConnection, paramT, paramObjectCache);
                localKeyHolder = null;
                break label84;
              }
              boolean bool2 = this.idField.isGeneratedId();
              localKeyHolder = null;
              if (!bool2)
                break label84;
              localKeyHolder = null;
              if (n == 0)
                break label84;
              localKeyHolder = new KeyHolder(null);
              break label84;
              Object localObject4 = localFieldType3.extractRawJavaFieldValue(paramT);
              if ((localObject4 != null) && (localFieldType3.getForeignIdField().isObjectsFieldValueDefault(localObject4)))
                localFieldType3.createWithForeignDao(localObject4);
            }
          }
        }
      }
      catch (SQLException localSQLException1)
      {
        label143: throw SqlExceptionUtil.create("Unable to run insert stmt on object " + paramT + ": " + this.statement, localSQLException1);
      }
    }
    Object[] arrayOfObject = getFieldObjects(paramT);
    int i = this.versionFieldTypeIndex;
    Object localObject1 = null;
    if (i >= 0)
    {
      Object localObject2 = arrayOfObject[this.versionFieldTypeIndex];
      localObject1 = null;
      if (localObject2 == null)
      {
        FieldType localFieldType2 = this.argFieldTypes[this.versionFieldTypeIndex];
        localObject1 = localFieldType2.moveToNextValue(null);
        arrayOfObject[this.versionFieldTypeIndex] = localFieldType2.convertJavaFieldToSqlArgValue(localObject1);
      }
    }
    int j;
    Number localNumber;
    try
    {
      j = paramDatabaseConnection.insert(this.statement, arrayOfObject, this.argFieldTypes, localKeyHolder);
      logger.debug("insert data with statement '{}' and {} args, changed {} rows", this.statement, Integer.valueOf(arrayOfObject.length), Integer.valueOf(j));
      if (arrayOfObject.length > 0)
        logger.trace("insert arguments: {}", arrayOfObject);
      if (j <= 0)
        break label611;
      if (localObject1 != null)
        this.argFieldTypes[this.versionFieldTypeIndex].assignField(paramT, localObject1, false, null);
      if (localKeyHolder == null)
        break label567;
      localNumber = localKeyHolder.getKey();
      if (localNumber == null)
        throw new SQLException("generated-id key was not set by the update call");
    }
    catch (SQLException localSQLException2)
    {
      logger.debug("insert data with statement '{}' and {} args, threw exception: {}", this.statement, Integer.valueOf(arrayOfObject.length), localSQLException2);
      if (arrayOfObject.length > 0)
        logger.trace("insert arguments: {}", arrayOfObject);
      throw localSQLException2;
    }
    if (localNumber.longValue() == 0L)
      throw new SQLException("generated-id key must not be 0 value");
    assignIdValue(paramT, localNumber, "keyholder", paramObjectCache);
    label567: if ((paramObjectCache != null) && (foreignCollectionsAreAssigned(this.tableInfo.getForeignCollections(), paramT)))
    {
      Object localObject3 = this.idField.extractJavaFieldValue(paramT);
      paramObjectCache.put(this.clazz, localObject3, paramT);
    }
    label611: return j;
  }

  private static class KeyHolder
    implements GeneratedKeyHolder
  {
    Number key;

    public void addKey(Number paramNumber)
      throws SQLException
    {
      if (this.key == null)
      {
        this.key = paramNumber;
        return;
      }
      throw new SQLException("generated key has already been set to " + this.key + ", now set to " + paramNumber);
    }

    public Number getKey()
    {
      return this.key;
    }
  }
}

/* Location:           C:\Users\user\Desktop\dd28_pcdd720\classes_dex2jar.jar
 * Qualified Name:     com.j256.ormlite.stmt.mapped.MappedCreate
 * JD-Core Version:    0.6.2
 */